<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <!--[if lt IE 9]>
    <script type="text/javascript" src="http://libs.useso.com/js/html5shiv/3.7/html5shiv.min.js"></script>
    <script type="text/javascript" src="http://libs.useso.com/js/respond.js/1.4.2/respond.min.js"></script>
    <script type="text/javascript" src="http://cdn.bootcss.com/css3pie/2.0beta1/PIE_IE678.js"></script>
    <![endif]-->
    <link rel="stylesheet" type="text/css" href="../../static/h-ui/css/H-ui.min.css"/>
    <link rel="stylesheet" type="text/css" href="../../static/h-ui.admin/css/H-ui.admin.css"/>
    <link type="text/css" rel="stylesheet" href="../../static/font/font-awesome.min.css"/>
    <link href="../../css/easyui/icon.css" rel="stylesheet" type="text/css"/>
    <link href="../../css/easyui/default/easyui.css" rel="stylesheet" type="text/css"/>
    <!--[if IE 7]>
    <link href="http://www.bootcss.com/p/font-awesome/assets/css/font-awesome-ie7.min.css" rel="stylesheet"
          type="text/css"/>
    <![endif]-->
    <title>添加用户</title>
</head>
<body>
<div class="pd-20">
    <div class="Huiform">
        <form action="" method="post" id="formData">
            <input type="hidden" name="userId" id="userId">
            <table class="table table-bg">
                <tbody>
                <tr>
                    <th width="100" class="text-r"><span class="c-red">*</span> 用户名：</th>
                    <td><input type="text" style="width:200px" class="input-text" value="" placeholder=""
                               id="useSysName" name="useSysName" datatype="*2-16" nullmsg="用户名不能为空"></td>
                </tr>
                <tr>
                    <th width="100" class="text-r"><span class="c-red">*</span> 姓名：</th>
                    <td><input type="text" style="width:200px" class="input-text" value="" placeholder="" id="userName"
                               name="userName" datatype="*2-16" nullmsg="用户名不能为空"></td>
                </tr>
                <tr>
                    <th width="100" class="text-r"><span class="c-red">*</span> 所属部门：</th>
                    <td>
                        <input type="text" style="width:200px" class="easyui-combotree" value="" placeholder=""
                               id="showId"
                               name="shouwId" datatype="*2-16">
                        <input type="hidden" name="deptId" id="deptId">
                    </td>

                </tr>
                <tr>
                    <th class="text-r"><span class="c-red">*</span> 性别：</th>
                    <td><label>
                        <input name="gender" type="radio" id="six_1" value="男" checked>
                        男</label>
                        <label>
                            <input type="radio" name="gender" value="女" id="six_0">
                            女</label></td>
                </tr>
                <tr>
                    <th class="text-r"><span class="c-red">*</span> 手机：</th>
                    <td><input type="text" style="width:300px" class="input-text" value="" placeholder=""
                               id="mobilePhone" name="mobilePhone"></td>
                </tr>
                <tr>
                    <th class="text-r">邮箱：</th>
                    <td><input type="text" style="width:300px" class="input-text" value="" placeholder="" id="email"
                               name="email"></td>
                </tr>
                <tr>
                    <th class="text-r">地址：</th>
                    <td><input type="text" style="width:300px" class="input-text" value="" placeholder="" id="address"
                               name="address"></td>
                </tr>
                <tr>
                    <th></th>
                    <td></td>
                </tr>
                </tbody>
            </table>
        </form>
        <button class="btn btn-success radius" type="" id="save"><i class="icon-ok"></i> 确定</button>
    </div>
</div>
<script type="text/javascript" src="../../lib/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="../../lib/jquery/1.9.1/jquery.js"></script>
<script type="text/javascript" src="../../lib/datatables/1.10.0/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="../../js/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="../../js/Validform_v5.3.2_min.js"></script>
<script type="text/javascript" src="../../h-ui/js/H-ui.js"></script>
<script type="text/javascript" src="../../h-ui.admin/js/H-ui.admin.js"></script>
<script type="text/javascript">
    $(".Huiform").Validform();
</script>
<script>
    window.onload = (function () {
      //  $('#showId').combotree();
        var userid = getQueryVariable("userid")
        var jsonArray = [];
        if (userid != null && userid != "") {
            var obj = {'column': "user_id", 'type': 'eq', "value": userid};
            jsonArray.push(obj)
        }
        $.ajax({
            type: 'POST',
            url: '/user/find',
            dataType: 'json',
            data: {paramData: JSON.stringify(jsonArray)},
            success: function (data) {
                //alert(data.data.toString());
                cpJsonToForm(data.data, "formData");
                comboTree();
            }
        })
    });

    $("#save").click(function () {
        var t = $('#showId').combotree('tree');	// get the tree object
        var n = t.tree('getSelected');		// get selected node
        //  alert(n.id);
        $("#deptId").val(n.id);
        var userId = $("#userId").val();
        if (userId != null && userId != "") {
            save(1);
        } else {
            save(0);
        }

    })

    function save(type) {
        var url;
        if (type == 0) {
            url = '/user/add';
        } else {
            url = '/user/update';
        }
        var formObject = {};
        var formArray = $("#formData").serializeArray();
        $.each(formArray, function (i, item) {
            formObject[item.name] = item.value;
        });
        //  alert(JSON.stringify(formObject));
        $.ajax({
            url: url,
            type: "post",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(formObject),
            dataType: "json",
            success: function (data) {
                alert("保存成功！");
                window.opener.loadlist();
                window.close();
            },
            error: function () {
                console.log("提交ajax函数异常");
            },

        });
    }

    function getQueryVariable(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) {
                return pair[1];
            }
        }
        return (false);
    }

    var cpJsonToForm = function (json, formId) {
        var serializeArray = $("#" + formId).serializeArray();
        for (var i = 0; i < serializeArray.length; i++) {
            var name = serializeArray[i].name;
            if (json[name]) {
                var value = json[name];
                $("[name='" + name + "']").val(value);
            }
        }
    };

    function comboTree() {
        $.ajax({
            url: "/common/selectDept",
            type: "POST",
            dataType: "json",
            success: function (data) {
                $('#showId').combotree(
                    'loadData',
                    eval(data.msg));
                var checkV = ($("#deptId").val());
                if (checkV != null && checkV != "") {
                    $('#showId').combotree('setValue', checkV);
                }

            },
            error: function () {
                alert("error")
            }
        });
    }
</script>
</body>
</html>
